package com.lighthouse.repo.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.lighthouse.common.TreeSelectKnowledge;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lighthouse.repo.mapper.KnowledgeRepoBaseInfoMapper;
import com.lighthouse.repo.domain.KnowledgeRepoBaseInfo;
import com.lighthouse.repo.service.IKnowledgeRepoBaseInfoService;

/**
 * 目录信息Service业务层处理
 * 
 * @author ysz
 * @date 2023-05-05
 */
@Service
public class KnowledgeRepoBaseInfoServiceImpl implements IKnowledgeRepoBaseInfoService 
{
    @Autowired
    private KnowledgeRepoBaseInfoMapper knowledgeRepoBaseInfoMapper;

    /**  根目录父级id  */
    private static final String ROOT_PARENT_ID = "0";

    /**
     * 查询目录信息
     * 
     * @param repoId 目录信息主键
     * @return 目录信息
     */
    @Override
    public KnowledgeRepoBaseInfo selectKnowledgeRepoBaseInfoByRepoId(String repoId)
    {
        return knowledgeRepoBaseInfoMapper.selectKnowledgeRepoBaseInfoByRepoId(repoId);
    }

    /**
     * 查询目录信息列表
     * 
     * @param knowledgeRepoBaseInfo 目录信息
     * @return 目录信息
     */
    @Override
    public List<KnowledgeRepoBaseInfo> selectKnowledgeRepoBaseInfoList(KnowledgeRepoBaseInfo knowledgeRepoBaseInfo)
    {
        return knowledgeRepoBaseInfoMapper.selectKnowledgeRepoBaseInfoList(knowledgeRepoBaseInfo);
    }

    /**
     * 新增目录信息
     * 
     * @param knowledgeRepoBaseInfo 目录信息
     * @return 结果
     */
    @Override
    public int insertKnowledgeRepoBaseInfo(KnowledgeRepoBaseInfo knowledgeRepoBaseInfo)
    {
        knowledgeRepoBaseInfo.setCreateInfo();
        int rows = knowledgeRepoBaseInfoMapper.insertKnowledgeRepoBaseInfo(knowledgeRepoBaseInfo);
        if (rows > 0) {
            
        }
        return rows;
    }

    /**
     * 修改目录信息
     * 
     * @param knowledgeRepoBaseInfo 目录信息
     * @return 结果
     */
    @Override
    public int updateKnowledgeRepoBaseInfo(KnowledgeRepoBaseInfo knowledgeRepoBaseInfo)
    {
        knowledgeRepoBaseInfo.setUpdateTime(DateUtils.getNowDate());
        return knowledgeRepoBaseInfoMapper.updateKnowledgeRepoBaseInfo(knowledgeRepoBaseInfo);
    }

    /**
     * 批量删除目录信息
     * 
     * @param repoIds 需要删除的目录信息主键
     * @return 结果
     */
    @Override
    public int deleteKnowledgeRepoBaseInfoByRepoIds(String[] repoIds)
    {
        return knowledgeRepoBaseInfoMapper.deleteKnowledgeRepoBaseInfoByRepoIds(repoIds);
    }

    /**
     * 删除目录信息信息
     * 
     * @param repoId 目录信息主键
     * @return 结果
     */
    @Override
    public int deleteKnowledgeRepoBaseInfoByRepoId(String repoId)
    {
        return knowledgeRepoBaseInfoMapper.deleteKnowledgeRepoBaseInfoByRepoId(repoId);
    }

    /**
     * 查询目录树
     * @author ysz
     * @param
     * @return List
     **/
    @Override
    public List<TreeSelectKnowledge> selectKnowledgeRepoBaseInfoTree() {
        List<KnowledgeRepoBaseInfo> knowledgeRepoBaseInfos = knowledgeRepoBaseInfoMapper.selectKnowledgeRepoBaseInfoList(new KnowledgeRepoBaseInfo());
        return buildRepoTreeSelect(knowledgeRepoBaseInfos);
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param knowledgeRepoBaseInfos 目录列表
     * @return 下拉树结构列表
     */
    public List<TreeSelectKnowledge> buildRepoTreeSelect(List<KnowledgeRepoBaseInfo> knowledgeRepoBaseInfos)
    {
        List<KnowledgeRepoBaseInfo> repoTrees = buildRepoTree(knowledgeRepoBaseInfos);
        return repoTrees.stream().map(TreeSelectKnowledge::new).collect(Collectors.toList());
    }

    /**
     * 构建前端所需要树结构
     *
     * @param knowledgeRepoBaseInfos 目录列表
     * @return 树结构列表
     */
    public List<KnowledgeRepoBaseInfo> buildRepoTree(List<KnowledgeRepoBaseInfo> knowledgeRepoBaseInfos)
    {
        List<KnowledgeRepoBaseInfo> returnList = new ArrayList<KnowledgeRepoBaseInfo>();
        List<String> tempList = knowledgeRepoBaseInfos.stream().map(KnowledgeRepoBaseInfo::getRepoId).collect(Collectors.toList());
        for (KnowledgeRepoBaseInfo repo : knowledgeRepoBaseInfos)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(repo.getParentId()))
            {
                recursionFn(knowledgeRepoBaseInfos, repo);
                returnList.add(repo);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = knowledgeRepoBaseInfos;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<KnowledgeRepoBaseInfo> list, KnowledgeRepoBaseInfo t)
    {
        // 得到子节点列表
        List<KnowledgeRepoBaseInfo> childList = getChildList(list, t);
        t.setChildren(childList);
        for (KnowledgeRepoBaseInfo tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<KnowledgeRepoBaseInfo> getChildList(List<KnowledgeRepoBaseInfo> list, KnowledgeRepoBaseInfo t)
    {
        List<KnowledgeRepoBaseInfo> tlist = new ArrayList<KnowledgeRepoBaseInfo>();
        Iterator<KnowledgeRepoBaseInfo> it = list.iterator();
        while (it.hasNext())
        {
            KnowledgeRepoBaseInfo n = (KnowledgeRepoBaseInfo) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().equals(t.getRepoId()))
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<KnowledgeRepoBaseInfo> list, KnowledgeRepoBaseInfo t)
    {
        return getChildList(list, t).size() > 0;
    }
}
